package com.app.jzgdirector.common.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;


import com.common.baselibrary.log.LogUtil;

import java.lang.ref.WeakReference;

/**
 * Created by zhangdesh on 16/10/19.
 * Dialog基类
 */
public abstract class BaseDialog extends Dialog {

    private static final String TAG = "BaseDialog";

    /**
     * 是否是手动Cancel掉Dialog的
     */
    protected boolean isManualCancelDialog = false;

    /**
     * cancel事件
     */
    protected OnDialogCancelListener mOnDialogCancelListener;

    /**
     * 对宿主Activity的持有
     */
    protected WeakReference<Activity> mHostActivity;

    public BaseDialog(Context context) {
        super(context);
        init(context);
    }

    public BaseDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    /**
     * 获取对话框布局
     */
    public abstract int getDialogLayout();

    /**
     * 获取对话框View
     */
    protected View getDialogView(Context context) {
        return null;
    }

    public void init(Context context) {
        if (context instanceof Activity) {
            mHostActivity = new WeakReference<>((Activity) context);
        }

        int dialogLayoutId = getDialogLayout();
        if (dialogLayoutId > 0) {
            setContentView(dialogLayoutId);
        } else {
            View dialogView = getDialogView(context);
            if (dialogView != null) {
                setContentView(dialogView);
            }
        }

        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                if (mOnDialogCancelListener != null && !isManualCancelDialog) {
                    //非主动cancel的则执行cancel回调：如返回键导致cancel的
                    mOnDialogCancelListener.onDialogCancelCallback();
                }
            }
        });
    }

    @Override
    public void show() {
        LogUtil.i(TAG, "show");
        isManualCancelDialog = false;
        if (mHostActivity != null && (mHostActivity.get() == null || mHostActivity.get().isFinishing())) {
            LogUtil.i(TAG, "show, activity reference is exist and is release.");
            return;
        }
        super.show();
    }

    @Override
    public void dismiss() {
        LogUtil.i(TAG, "dismiss");
        if (mHostActivity != null && (mHostActivity.get() == null || mHostActivity.get().isFinishing())) {
            LogUtil.i(TAG, "dismiss, activity reference is exist and is release.");
            return;
        }
        super.dismiss();
    }

    @Override
    public void cancel() {
        LogUtil.i(TAG, "cancel");
        super.cancel();
    }

    /**
     * 手动cancel
     */
    public void manualCancel() {
        LogUtil.i(TAG, "manualCancel");
        this.isManualCancelDialog = true;
        super.cancel();
    }

    /**
     * 设置Cancel事件回调
     */
    public void setOnCancelListener(OnDialogCancelListener onDialogCancelListener) {
        this.mOnDialogCancelListener = onDialogCancelListener;
    }

    public interface OnDialogCancelListener {
        void onDialogCancelCallback();
    }
}
